<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">{{ $t("i18n.login_title") }}</div>
      <el-form
        :model="param"
        :rules="rules"
        ref="login"
        label-width="0px"
        class="ms-content"
      >
        <el-form-item prop="unitCode">
          <el-input v-model="param.unitCode" :placeholder="param.unitTip">
            <template #prepend>
              <el-button icon="el-icon-lx-home"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="account">
          <el-input v-model="param.account" :placeholder="param.accounttips">
            <template #prepend>
              <el-button icon="el-icon-user"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :placeholder="param.passwordtip"
            type="password"
            v-model="param.password"
            @keyup.enter="submitFormECB()"
          >
            <template #prepend>
              <el-button icon="el-icon-lock"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitFormECB()">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import aesVue from "../utils/aes.vue";
import reqVue from "../utils/req.vue";
import conf from "../utils/conf.vue";
export default {
  data() {
    return {
      param: {
        account: "",
        password: "",
        passwordtip: "请输入密码",
        accounttips: "请输入用户名",
        unitCode: "",
        unitTip: "请输入租户编码",
      },
      rules: {
        account: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        unitCode: [
          { required: true, message: "请输入租户编码", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.$store.commit("clearTags");
  },
  methods: {
    submitFormBCB() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          let data = this.param;
          delete data.unitTip;
          delete data.accounttips;
          delete data.passwordtip;
          var keys = conf.$secret;
          if (data.account != "admin") {
            data.password = aesVue.$CBCEncrypt(data.password, keys);
          }
          var parameter = aesVue.$CBCEncrypt(JSON.stringify(data), keys);
          reqVue
            .$Post(
              {
                tenantContent: parameter,
                tenantCode: data.unitCode,
                ClientId: "Taurent_ClientId",
              },
              "token",
              "auth"
            )
            .then(
              function(data) {
                if (data.status) {
                  const { body } = data;
                  localStorage.removeItem("gloabl_token");
                  localStorage.removeItem("refresh_Token");
                  localStorage.setItem("gloabl_token", body.access_token);
                  localStorage.setItem("refresh_Token", body.refresh_token);
                  localStorage.removeItem("key_language");
                  localStorage.setItem("key_language", body.language);
                  localStorage.removeItem("grant_type");
                  localStorage.setItem("grant_type", body.grant_type);
                  window.location.href = "/";
                } else {
                  this.$message.error(data.message);
                  return false;
                }
              }.bind(this)
            );
        } else {
          this.$message.error("请输入账号和密码");
          return false;
        }
      });
    },
    submitFormECB() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          let data = this.param;
          delete data.unitTip;
          delete data.accounttips;
          delete data.passwordtip;
          var keys = aesVue.$Generatekey(32);
          if (data.account != "admin") {
            data.password = aesVue.$Encrypt(data.password);
          }
          var parameter = aesVue.$Encrypt(JSON.stringify(data), keys);
          reqVue
            .$Post(
              {
                tenantContent: parameter,
                tenantSecret: keys,
                tenantCode: data.unitCode,
                ClientId: "Taurent_ClientId",
              },
              "token",
              "auth"
            )
            .then(
              function(data) {
                if (data.status) {
                  const { body } = data;
                  localStorage.removeItem("gloabl_token");
                  localStorage.removeItem("refresh_Token");
                  localStorage.setItem("gloabl_token", body.access_token);
                  localStorage.setItem("refresh_Token", body.refresh_token);
                  localStorage.removeItem("key_language");
                  localStorage.setItem("key_language", body.language);
                  localStorage.removeItem("grant_type");
                  localStorage.setItem("grant_type", body.grant_type);
                  window.location.href = "/";
                } else {
                  this.$message.error(data.message);
                  return false;
                }
              }.bind(this)
            );
        } else {
          this.$message.error("请输入账号和密码");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url(../assets/img/login-bg1.jpeg) !important;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 420px;
  margin: -190px 0 0 -185px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}
</style>
